package com.water.app.Impl.shopping;

import com.water.app.Exceptions.BalanceException;
import com.water.app.Exceptions.StockException;
import com.water.app.interfaces.shopping.ShoppingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingDeque;


class Consumer implements Runnable {
    private static final Logger log = LoggerFactory.getLogger(Consumer.class);
    private final BlockingDeque<ShoppingServiceImpl.GoodsIdListAndUserId> qeque;
    private final ShoppingService shoppingService;

    public Consumer(BlockingDeque<ShoppingServiceImpl.GoodsIdListAndUserId> qeque,ShoppingServiceImpl shoppingService) {
        this.qeque=qeque;
        this.shoppingService=shoppingService;
    }


    @Override
    public void run() {
        while (true){
            try {
                ShoppingServiceImpl.GoodsIdListAndUserId take = qeque.take();
                shoppingService.consumerPay(take.goodsIdList,take.getUserId(),take.getAddressId());
            } catch (InterruptedException | StockException | BalanceException e) {
                e.printStackTrace();
            }
        }
    }
}